package com.gvfish.dao;

import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;

import net.sf.dozer.util.mapping.DozerBeanMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.gvfish.core.DozerBeanMapperFactory;
import com.gvfish.core.Page;
import com.gvfish.entity.Article;
import com.gvfish.entity.Category;
import com.gvfish.entity.Details;

@Repository
public class ArticleDao extends CommonDao<Article, Key> {

	Logger log = Logger.getLogger(ArticleDao.class.getName());
	@Autowired
	private PersistenceManagerFactory persistenceManagerFactory;

	public Article getById(Key key) {
		PersistenceManager pm = getPersistenceManager();
		Article article = null;
		Article vo = new Article();
		try {
			article = pm.getObjectById(Article.class, key);
			if (article.getView() != null) {
				article.setView(article.getView() + 1);
			} else {
				article.setView(1);
			}

			DozerBeanMapper bm = DozerBeanMapperFactory.getMapper();
			bm.map(article, vo);
			// vo.getDetails().setText(new
			// Text(article.getDetails().getText().getValue()));
			vo.setDetails(article.getDetails());
		} finally {
			pm.close();
		}
		return vo;
	}

	@Override
	public PersistenceManager getPersistenceManager() {
		return persistenceManagerFactory.getPersistenceManager();
	}

	public Article save(Long categoryId, Article article) {
		PersistenceManager pm = getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Category category = pm.getObjectById(Category.class, categoryId);
			if (category.getArtCount() == null) {
				category.setArtCount(category.getArticles().size());
			}
			category.setArtCount(category.getArtCount() + 1);
			category.getArticles().add(article);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.commit();
			}
		}
		return article;
	}

	@Override
	public Class<Article> getTClass() {
		return Article.class;
	}

	@SuppressWarnings("unchecked")
	public List<Article> findLastedArt() {
		PersistenceManager pm = getPersistenceManager();
		Query query = pm.newQuery(Article.class);
		query.setOrdering("editDate desc");
		query.setRange(0, 10);
		return (List<Article>) query.execute();
	}

	@SuppressWarnings("unchecked")
	public List<Article> findHotArts() {
		List<Article> result = null;
		PersistenceManager pm = getPersistenceManager();
		try {
			Query query = pm.newQuery(Article.class);
			query.setOrdering("editDate desc");
			query.setRange(0, 10);
			List<Article> src = (List<Article>) query.execute();
			result = src.subList(0, src.size());
		} finally {
			pm.close();
		}
		return result;

	}

	@SuppressWarnings("unchecked")
	public Page<Article> getTest(Long categoryId, Page<Article> page) {
		Key categoryKey = KeyFactory.createKey(Category.class.getSimpleName(), categoryId);
		PersistenceManager pm = getPersistenceManager();
		Query query = pm.newQuery(Article.class, "categoryKey == cKey");
		query.declareParameters("com.google.appengine.api.datastore.Key cKey");
		int start = page.getPageSize() * (page.getPageNo() - 1);
		int end = start + page.getPageSize();
		System.out.println("start" + start);
		System.out.println("end" + end);
		query.setRange(start, end);
		List<Article> result = Collections.emptyList();
		try {
			result = (List<Article>) query.execute(categoryKey);
		} catch (Exception e) {
			System.out.println("in e");
			System.out.println(e);
			e.printStackTrace();
		}
		page.setResult(result);
		return page;
	}

	public List<Article> getByCategory(Long categoryId) {
		Key categoryKey = KeyFactory.createKey(Category.class.getSimpleName(), categoryId);
		PersistenceManager pm = getPersistenceManager();
		Query query = pm.newQuery(Article.class, "categoryKey == cKey");
		query.declareParameters("com.google.appengine.api.datastore.Key cKey");
		query.setOrdering("editDate desc");
		query.setRange(0, 10);
		List<Article> result = Collections.emptyList();
		try {
			result = (List<Article>) query.execute(categoryKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
